#include "../../include/transformation/project.hpp"

namespace transformation{
    // 这是生成正交投影的矩阵
    Matrix44 Project::OrthographicProject(int top,int bottom,int left,int right,int near,int far){
        // 第一步是将视锥体的中点移动到原点
        Matrix44 move(
            1,0,0,-(left+right)/2.0,
            0,1,0,-(top+bottom)/2.0,
            0,0,1,-(near+far)/2.0,
            0,0,0,1
        );
        move.printMatrix();
        // 第二步将长方体缩放为正方体
        Matrix44 scale(
            2.0/(right-left),0,0,0,
            0,2.0/(top-bottom),0,0,
            0,0,2.0/(far-near),0,
            0,0,0,1
        );
        scale.printMatrix();
        return scale*move;
    }
    // 接下来是透视投影，透视投影增加了一个步骤，就是将视锥体压缩为长方体，
    Matrix44 Project::PerspectiveProject(int top,int bottom,int left,int right,int near,int far){
        // 第一步，首先进行压缩
        Matrix44 compress(
            near,0,0,0,
            0,near,0,0,
            0,0,near+far,-(near*far),
            0,0,1,0
        );
        // 第二步，进行正交投影
        Matrix44 orthographic = OrthographicProject(top,bottom,left,right,near,far);
        return orthographic*compress;
    }
    Matrix44 Project::PerspectiveProject(float fov,float aspect,float near,float far){
        float top = near*tan(fov/2.0);
        float bottom = -top;
        float left = bottom*aspect;
        float right = top*aspect;
        return PerspectiveProject(top,bottom,left,right,near,far);
    }

}